package com.msg.xml;

import java.util.ArrayList;
import java.util.List;

/**
 * this class read the capacity informatio nof a event from XML file
 * and create the Capacity records with different event code, ticket type and ticket level
 * @author jeson9603
 *
 */
public class Capacity_Manipulator extends Table_Manipulator {

	private List<CapacityRecord> records;
	private List<List<String>> capacityData = new ArrayList<List<String>>();
	
	
	public Capacity_Manipulator(Property prop) {
		super(prop);
	}
	/**
	 * get the event code, ticket type and capacity data from XML file within the Capacity field
	 * @return
	 */
	@Override
	protected String getSpecificData() {
		//pinpoint the tag *** CAPACITY first
		XMLhandler.findNodeByTag("***       CAPACITY");
		//move the cursor forward by one positions
		XMLhandler.moveCursorForward();
		while(true) {
			ArrayList<String> contents = XMLhandler.getDataList();
			//reach the end, terminate the iteration
			if(contents.get(0).compareTo("TOTAL") == 0)
				break;
			//jump over the blank line
			if(contents.get(0).compareTo("") == 0)
				continue;
			List<String> dataRow = new ArrayList<String>();
			dataRow.add(contents.get(0));
			dataRow.add(TableUtility.getTicketType(contents.get(0)));
			for(int i = 4; i < contents.size(); i++) {
				if(contents.get(i).compareTo("") == 0)
					break;
				dataRow.add(contents.get(i));
			}
			capacityData.add(dataRow);
		}
		return null;
	}

	
	/**
	 * transform the raw data of capacity read from XML to the CapacityRecords
	 * @return
	 */
	@Override
	protected String update() {
		for(int i = 0; i < capacityData.size(); i++) {
			List<String> dataRow = capacityData.get(i);
			for(int j = 0; j < prop.getTICKETLEVEL_NUM(); j++) {
				CapacityRecord newRecord = new CapacityRecord();
				newRecord.setEventCode(dataRow.get(0));
				newRecord.setTicketType(dataRow.get(1));
				newRecord.setTicketLevel((char)('A' + j));
				newRecord.setCapacity(dataRow.get(2+j));
				records.add(newRecord);
			}
		}
		return null;
	}

	
	/**
	 * give an empty capacity records list to capacity manipulator so that it can add records in this object
	 * @param o, a list of capacity records
	 */
	@Override
	@SuppressWarnings("unchecked")
	protected void pushData(Object o) {
		records = (List<CapacityRecord>)o;
	}

}
